package com.zmm.member.controller;

import com.zmm.common.annotation.SysLog;
import com.zmm.common.base.model.Result;
import com.zmm.common.dict.enums.RegionVersion;
import com.zmm.common.exception.handle.BusinessException;
import com.zmm.common.valid.AddGroup;
import com.zmm.member.self.util.CheckCustomerAddressUtils;
import com.zmm.member.service.CustomerAddressService;
import com.zmm.member.submit.vo.OrderAddressVo;
import com.zmm.util.verify.BindingResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 900045
 * @description:
 * @name AddressController
 * @date By 2021-07-23 14:29:22
 */
@Controller
@Slf4j
@RequestMapping("/customer/address")
public class CustomerAddressController {
	
	@Resource
	private CustomerAddressService customerAddressService;
	

	/**
	 * 控制器 - 地址管理 - 查询指定用户绑定的地址信息
	 * @author: 900045
	 * @date: 2021-07-23 16:55:08
	 * @throws 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@ResponseBody
	@GetMapping(value = "/query/list")
	public Result queryCustomerAddressList(){
		Integer customerId = 3;
		List<OrderAddressVo> orderAddressVoList = customerAddressService.queryCustomerAddressListByCustomerId(customerId);
		return Result.ok().setData(orderAddressVoList);
	}

	/**
	 * 去到添加地址的页面
	 * @author: 900045
	 * @date: 2021-08-06 11:20:04
	 * @throws 
	
	 * @return: java.lang.String
	 **/
	@SysLog(value = "新增用户地址页面", location = "toAddAddress", service = "service-member")
	@RequestMapping(value = "/to/add",method = RequestMethod.GET)
	public String toAddAddress(){
		return "add_address";
	}
	
	/**
	 * 用户新增地址 --- 控制器
	 * @author: 900045
	 * @date: 2021-08-04 15:30:10
	 * @throws 
	 * @param address: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Result saveCustomerAddress(@Validated({AddGroup.class}) OrderAddressVo address, BindingResult result) throws BusinessException {
		

		// 1.表达校验
		if (result.hasErrors()){
			//  Model model
			//model.addAttribute("orders",result)
			return BindingResultUtil.paramVerify(result);
		}

		// 2.方式二校验
		CheckCustomerAddressUtils.baseCheck(address);
		
		Integer customerId = 1;
		address.setVersion(RegionVersion.V2.version);
		boolean add = customerAddressService.addCustomerAddress(address,customerId);
		if (add){
			return Result.ok();
		} else {
			return Result.error();
		}
	}
}
